package com.lxujia.lib;

import java.io.FileInputStream;
import java.io.IOException;

public class BaselineJpeg {
    public static boolean isBaseline(String file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        CountedDataInputStream dataStream = new CountedDataInputStream(inputStream);
        if (dataStream.readShort() != JpegHeader.SOI) {
            return false;
        }

        short marker = dataStream.readShort();
        while (marker != JpegHeader.EOI
                && !JpegHeader.isSofMarker(marker)) {
            int length = dataStream.readUnsignedShort();

            if (length < 2 || (length - 2) != dataStream.skip(length - 2)) {
                return false;
            }
            marker = dataStream.readShort();
        }
        if (marker == JpegHeader.SOF0) {
            return true;
        }
        return false;
    }

}